const app = getApp()
Page({
  data: {
    // 组件所需的参数
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '6寸照片', //导航栏 中间的 标题
    },
    ratio: 102 / 152,
    type: 0,
    oriUrl: [],
    imageUrl: [],
    isColours: true,
    id: 1,
    editIndex: -1
  },
  onLoad: function (options) {
    this.setData({
      height: app.globalData.height
    })
  },
  uploadImage: function () {
    let that = this;
    if (9 <= this.data.imageUrl.length) {
      wx.showToast({
        title: '最多上传9张照片',
        icon: 'none'
      })
      return false;
    }
    wx.chooseImage({
      count: 9,
      sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
      // sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      complete: (res) => {
        let oriUrl = that.data.oriUrl.concat(res.tempFilePaths);
        that.data.oriUrl = oriUrl;
        that.setData({ imageUrl: oriUrl })
        // res.tempFilePaths.forEach(url => {
        //   that.setData({ originUrl: url });
        // })
      },
    })
  },
  getCropperImg: function (e) {
    // wx.saveImageToPhotosAlbum({
    //   filePath: e.detail.url,
    // })
    if (this.data.editIndex > -1) {
      this.data.imageUrl[this.data.editIndex] = e.detail.url;
      this.data.editIndex = -1;
    } else {
      this.data.imageUrl.push(e.detail.url);
    }
    this.setData({
      originUrl: '',
      imageUrl: this.data.imageUrl
    })
  },
  editUploadImage: function (event) {
    const index = event.currentTarget.dataset.index;
    this.data.editIndex = index;
    const originUrl = this.data.oriUrl[index];
    this.setData({ originUrl: originUrl });
  },
  closeImage: function (event) {
    const index = event.currentTarget.dataset.index;
    this.data.imageUrl.splice(index, 1);
    this.setData({ imageUrl: this.data.imageUrl });
  },
  submitAdvice: function () {
    if (this.data.imageUrl.length <= 0) {
      wx.showToast({
        title: '请上传最少一张照片',
        icon: 'none'
      })
      return false;
    }
    const that = this;
    wx.showModal({
      title: '为了防止图片被过度裁剪，请确认你已经预览过所有图片',
      confirmText: '确定打印',
      cancelText: '返回预览',
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/modelA/pages/drawings_printing/picplace_order/picplace_order?pics=' + that.data.imageUrl.join(',') + '&title=' + '6寸照片' + '&type=' + that.data.type + '&ccinfo=' + '10.2*15.2cm' + '&isColours=' + that.data.isColours + '&id=' + that.data.id,
          })
        }
      }
    })
  }
})